﻿#include <QStyleFactory>
#include <QMessageBox>
#include "CConnectDevDlg.h"
#include "CGlobal.h"
#include "CModelMng.h"
#include "Public.h"

CConnectDevDlg::CConnectDevDlg(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("设备绑定"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	setStyleSheetFile();
	initConnect();
	initUi();
}

CConnectDevDlg::~CConnectDevDlg()
{}

void CConnectDevDlg::setData(QString sDevList)
{
	//模版信息树补全
	if (m_pTreeInfoModel != nullptr)
	{
		for (int i = 0; i < m_pTreeInfoModel->rowCount(); i++)
		{
			QStandardItem *pItem = m_pTreeInfoModel->item(i);
			delete pItem;
			pItem = nullptr;
		}
		m_pTreeInfoModel->setRowCount(0);
	}
	int nIndex = 0;
	for (int i = 0; i < CModelMng::instance().m_vecDev.size(); i++)
	{
		HKAIDevice* pDev = CModelMng::instance().m_vecDev[i];
		QString sDevID = pDev->m_sDevGuid;
		QString sDevName = pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName;
		QStandardItem *pItem = new QStandardItem(sDevName);
		pItem->setData(sDevID, 300);
		pItem->setCheckable(true);
		if (sDevList.contains(sDevID))
		{
			pItem->setCheckState(Qt::Checked);
		}
		m_pTreeInfoModel->setItem(nIndex, 0, pItem);
		nIndex++;
	}
}

QString CConnectDevDlg::getDevList()
{
	return m_sResult;
}

void CConnectDevDlg::initConnect()
{
	connect(ui.pushButtonAllCheck, &QPushButton::clicked, this, &CConnectDevDlg::slotAllCheck);
	connect(ui.pushButtonDisCheck, &QPushButton::clicked, this, &CConnectDevDlg::slotDisCheck);
	connect(ui.pushButtonOK, &QPushButton::clicked, this, &CConnectDevDlg::slotPushButtonOk);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CConnectDevDlg::slotPushButtonClose);
}

void CConnectDevDlg::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CConnectDevDlg::initUi()
{
	m_pTreeInfoModel = new QStandardItemModel();
	ui.treeView->setModel(m_pTreeInfoModel);
	ui.treeView->setStyle(QStyleFactory::create("windows"));
	ui.treeView->setSelectionMode(QAbstractItemView::SingleSelection);
	ui.treeView->setFrameShape(QFrame::NoFrame);
	ui.treeView->header()->hide();
	ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
	ui.treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui.treeView->setFocusPolicy(Qt::NoFocus);
	int nIndex = 0;
	for (int i = 0; i < CModelMng::instance().m_vecDev.size(); i++)
	{
		HKAIDevice* pDev = CModelMng::instance().m_vecDev[i];
		QString sDevID = pDev->m_sDevGuid;
		QString sDevName = pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName;
		QStandardItem *pItem = new QStandardItem(sDevName);
		pItem->setData(sDevID, 300);
		pItem->setCheckable(true);
		m_pTreeInfoModel->setItem(nIndex, 0, pItem);
	}
	ui.treeView->expandAll();
}

void CConnectDevDlg::slotAllCheck()
{
	// 遍历并勾选所有二级节点
	for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
	{
		QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
		item->setCheckState(Qt::Checked);
	}
}

void CConnectDevDlg::slotDisCheck()
{
	// 遍历并勾选所有二级节点
	for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
	{
		QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
		item->setCheckState(Qt::Unchecked);
	}
}

void CConnectDevDlg::slotPushButtonOk()
{
	//提醒一下
	int nRet = messageBox(3, A2T("是否确认绑定这些设备?"));
	if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
	{
		return;
	}

	// 遍历并取消勾选所有二级节点
	for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
	{
		QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
		//勾选了
		if (item->checkState() == Qt::Checked)
		{
			m_sResult += item->data(300).toString();
			m_sResult += ",";
		}
	}
	//刷新界面
	messageBox(0, A2T("操作成功"));
	this->accept();
}

void CConnectDevDlg::slotPushButtonClose()
{
	this->reject();
}
